import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.List;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/4 16:22
 * 一些测试 Demo
 */
public class Main {

    static ChromeOptions options;

    static {
        options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
    }

    // 主方法
    public static void main(String[] args) throws InterruptedException, IOException {
//        test01();
//        test02();
//        test03();
//        test04();
//        test05();
//        test06();
        test07();
    }

    // 判断百度网页的某一元素是否可以被点击
    private static void test01() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 判断某一元素是否可以被点击
        // 显式等待
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(3000));
        wait.until(ExpectedConditions.elementToBeClickable(
                By.cssSelector("#bottom_layer > div > p:nth-child(7) > a")));
        // 判断 title
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    // 浏览器前进/后退/刷新
    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 搜索 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        // 后退
        webDriver.navigate().back();
        Thread.sleep(3000);
        // 刷新
        webDriver.navigate().refresh();
        Thread.sleep(3000);
        // 前进
        webDriver.navigate().forward();
        // 刷新
        webDriver.navigate().refresh();
    }

    // 滚动条 与 浏览器最大化
    private static void test03() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 搜索 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        // 操作滚动条
        Thread.sleep(3000);
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=1000");
        Thread.sleep(3000);
        webDriver.manage().window().maximize(); // 最大化
        Thread.sleep(3000);
        webDriver.manage().window().fullscreen(); // 全屏
        Thread.sleep(3000);
    }

    // 键盘操作
    private static void test04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 搜索 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        // control + A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        Thread.sleep(3000);
        // control + X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        Thread.sleep(3000);
        // control + V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
    }

    // 鼠标右击
    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 搜索 521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        // 找到图片按钮
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        // 鼠标右击
        Actions actions = new Actions(webDriver);
        Thread.sleep(3000);
        actions.moveToElement(element).contextClick().perform();
    }

    // 点击复选框
    private static void test06() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("http://localhost:63342/test2023/static/test01.html?_ijt=sq9uuhaiovitjvlpd8ej8c3550&_ij_reload=RELOAD_ON_SAVE");
        Thread.sleep(3000);
        List<WebElement> inputs = webDriver.findElements(By.cssSelector("input"));
        for (WebElement input : inputs) {
            String type = input.getAttribute("type");
            if (type.equals("checkbox")) {
                input.click();
            }
        }
    }

    // 截图
    private static void test07() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        // 截图
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("src/main/resources/static/test.png"));
    }
}
